package com.immoc.demo.config.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.beans.PropertyVetoException;

/**
 * @Author Administrator
 * @Description 数据源配置连接池
 * @Date 2018/4/7 14:19
 * @Version 1.0
 **/
@Configuration
@MapperScan("com.immoc.demo.dao")  //指定dao接口扫描路径，无需@repository注解，参考 http://412887952-qq-com.iteye.com/blog/2392672
public class DataSourceConfiguration {
    @Value("${jdbc.driver}")
    private String jdbcDriver;
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.username}")
    private String jdbcUser;
    @Value("${jdbc.password}")
    private String jdbcPWD;

    @Bean(name = "dataSource")  //给该bean指定别名
    public ComboPooledDataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(jdbcDriver);
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setUser(jdbcUser);
        dataSource.setPassword(jdbcPWD);
        return dataSource;
    }
}
